/*抽象模块*/
#pragma once
#include<memory>
#include<functional>
#include"2.fields.hpp"

namespace rpcType
{
    //rpc基础消息
    class BaseMessage
    {
        public:
        using ptr = std::shared_ptr<BaseMessage>;
        //虚函数
        virtual ~BaseMessage()
        {}
        //设置id
        virtual void setId(std::string &id)
        {
            _rid = id;
        }
        virtual std::string rid()
        {
            return _rid;
        }
        //设置类型
        virtual void setMType(MType mtype)
        {
            _mtype = mtype;
        }
        virtual MType mtype()
        {
            return _mtype;
        }
        //序列化
        virtual std::string serialize() = 0;
        //反序列化
        virtual bool unserialize(const std::string &msg) = 0;
        virtual bool check() = 0;//检查
        private:
        MType _mtype;
        std::string _rid;
    };
    //缓冲区
    class BaseBuffer
    {
        public:
        using ptr = std::shared_ptr<BaseBuffer>;
        virtual size_t readableSize() = 0;//缓冲区可读大小
        virtual int32_t peekInt32() = 0; 
        virtual void retrieveInt32() = 0;//取出4字节
        virtual int32_t readInt32() = 0;//读4字节
        virtual std::string retrieveasString(size_t len) = 0;//取出len字符串
    };
    class BaseProtocol//协议
    {
        public:
        using ptr = std::shared_ptr<BaseProtocol>;
        //可处理数据
        virtual bool canProcessed(const BaseBuffer::ptr &buf) = 0;
        //正在处理消息
        virtual bool onMessage(const BaseBuffer::ptr &buf,BaseMessage::ptr&msg) = 0;
        //序列化消息
        virtual std::string serialize(const BaseMessage::ptr &msg) = 0;
    };
    //连接
    class BaseConnection
    {
        public:
        using ptr = std::shared_ptr<BaseConnection>;
        //发送消息
        virtual void send(const BaseMessage::ptr &msg) = 0;
        //关闭
        virtual void shutdown()=0;
        //连接
        virtual bool connected()=0;
    };
    //回调函数
    using ConnctionCallback = std::function<void(const BaseConnection::ptr&)>;//连接回调函数
    using CloseCallback = std::function<void(const BaseConnection::ptr&)>;//关闭回调函数
    using MessageCallback = std::function<void(const BaseConnection::ptr&,BaseMessage::ptr&)>;

    //服务
    class BaseServer
    {
        public:
        using ptr = std::shared_ptr<BaseServer>;
        //设置连接回调
        virtual void setConnectionCallback(const ConnctionCallback &cb)
        {
            _cb_connection = cb;
        } 

        virtual void setCloseCallback(const CloseCallback &cb)
        {
            _cb_close = cb;
        }
        //消息回调
        virtual void  setMessageCallback(const MessageCallback &cb)
        {
            _cb_message = cb;
        }
        protected:
        ConnctionCallback _cb_connection;
        CloseCallback _cb_close;
        MessageCallback _cb_message;   
    };
    //客户端
    class BaseClient
    {
        public:
        using ptr  = std::shared_ptr<BaseClient>;
        virtual void setConnectionCallback(const ConnctionCallback &cb)
        {
            _cb_connection = cb;
        } 

        virtual void setCloseCallback(const CloseCallback &cb)
        {
            _cb_close = cb;
        }
        //消息回调
        virtual void  setMessageCallback(const MessageCallback &cb)
        {
            _cb_message = cb;
        }
        virtual void connect() =0;//客户端连接
        virtual void shutdown()=0;//关闭客户端
        virtual bool send(const BaseMessage::ptr &msg)=0;//客户端发送消息
        virtual BaseConnection::ptr connection() = 0;//客户端当前持有连接对象
        virtual bool connected()=0;
        protected:
            ConnctionCallback _cb_connection;
            CloseCallback _cb_close;
            MessageCallback _cb_message;
    };
}